using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class Repository_MaterialDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public Repository_MaterialDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Repository_Material table.
		/// </summary>
		public void Insert(Repository_MaterialDTO repository_Material)
		{
			ValidationUtility.ValidateArgument("repository_Material", repository_Material);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idmaterial", repository_Material.Idmaterial),
				new SqlParameter("@idrepository", repository_Material.Idrepository),
				new SqlParameter("@amount", repository_Material.Amount),
				new SqlParameter("@remaining", repository_Material.Remaining)
			};

			repository_Material.Id = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Repository_MaterialInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the Repository_Material table.
		/// </summary>
		public void Update(Repository_MaterialDTO repository_Material)
		{
			ValidationUtility.ValidateArgument("repository_Material", repository_Material);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", repository_Material.Id),
				new SqlParameter("@idmaterial", repository_Material.Idmaterial),
				new SqlParameter("@idrepository", repository_Material.Idrepository),
				new SqlParameter("@amount", repository_Material.Amount),
				new SqlParameter("@remaining", repository_Material.Remaining)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Repository_MaterialUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the Repository_Material table by its primary key.
		/// </summary>
		public void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Repository_MaterialDelete", parameters);
		}

		/// <summary>
		/// Deletes a record from the Repository_Material table by a foreign key.
		/// </summary>
		public void DeleteAllByIdmaterial(int idmaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idmaterial", idmaterial)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_Repository_MaterialDeleteAllByIdmaterial", parameters);
		}

		/// <summary>
		/// Deletes a record from the Repository_Material table by a foreign key.
		/// </summary>
		public void DeleteAllByIdrepository(int idrepository)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idrepository", idrepository)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_Repository_MaterialDeleteAllByIdrepository", parameters);
		}

		/// <summary>
		/// Selects a single record from the Repository_Material table.
		/// </summary>
		public Repository_MaterialDTO Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Repository_MaterialSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the Repository_Material table.
		/// </summary>
		public string SelectJson(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "Repository_MaterialSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the Repository_Material table.
		/// </summary>
		public List<Repository_MaterialDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Repository_MaterialSelectAll"))
			{
				List<Repository_MaterialDTO> repository_MaterialDTOList = new List<Repository_MaterialDTO>();
				while (dataReader.Read())
				{
					Repository_MaterialDTO repository_MaterialDTO = MapDataReader(dataReader);
					repository_MaterialDTOList.Add(repository_MaterialDTO);
				}

				return repository_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the Repository_Material table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "Repository_MaterialSelectAll");
		}

		/// <summary>
		/// Selects all records from the Repository_Material table by a foreign key.
		/// </summary>
		public List<Repository_MaterialDTO> SelectAllByIdmaterial(int idmaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idmaterial", idmaterial)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_Repository_MaterialSelectAllByIdmaterial", parameters))
			{
				List<Repository_MaterialDTO> repository_MaterialDTOList = new List<Repository_MaterialDTO>();
				while (dataReader.Read())
				{
					Repository_MaterialDTO repository_MaterialDTO = MapDataReader(dataReader);
					repository_MaterialDTOList.Add(repository_MaterialDTO);
				}

				return repository_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the Repository_Material table by a foreign key.
		/// </summary>
		public List<Repository_MaterialDTO> SelectAllByIdrepository(int idrepository)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idrepository", idrepository)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_Repository_MaterialSelectAllByIdrepository", parameters))
			{
				List<Repository_MaterialDTO> repository_MaterialDTOList = new List<Repository_MaterialDTO>();
				while (dataReader.Read())
				{
					Repository_MaterialDTO repository_MaterialDTO = MapDataReader(dataReader);
					repository_MaterialDTOList.Add(repository_MaterialDTO);
				}

				return repository_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the Repository_Material table by a foreign key.
		/// </summary>
		public string SelectAllByIdmaterialJson(int idmaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idmaterial", idmaterial)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_Repository_MaterialSelectAllByIdmaterial", parameters);
		}

		/// <summary>
		/// Selects all records from the Repository_Material table by a foreign key.
		/// </summary>
		public string SelectAllByIdrepositoryJson(int idrepository)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idrepository", idrepository)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_Repository_MaterialSelectAllByIdrepository", parameters);
		}

		/// <summary>
		/// Creates a new instance of the Repository_MaterialDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private Repository_MaterialDTO MapDataReader(SqlDataReader dataReader)
		{
			Repository_MaterialDTO repository_MaterialDTO = new Repository_MaterialDTO();
			repository_MaterialDTO.Id = dataReader.GetInt32("id", 0);
			repository_MaterialDTO.Idmaterial = dataReader.GetInt32("idmaterial", 0);
			repository_MaterialDTO.Idrepository = dataReader.GetInt32("idrepository", 0);
			repository_MaterialDTO.Amount = dataReader.GetInt32("amount", 0);
			repository_MaterialDTO.Remaining = dataReader.GetInt32("remaining", 0);

			return repository_MaterialDTO;
		}

		#endregion
	}
}
